#pragma once
#include <vector>
#include <string>
#include "GridGenerator.h"
#include "PropertyKDTree.h"
#include "GridStructs.h"
#include <omp.h>

class PropertyMapper {
public:
    PropertyMapper();
    
    // 设置K近邻搜索的邻居数量
    static void setKNearestPoints(size_t k) { K_NEAREST_POINTS = k; }
    
    // 执行属性映射
    void process(const std::string& gridFile, 
                const std::string& propsFile,
                const std::vector<Point3D>& targetNodes,
                const std::vector<Tetrahedron>& targetElements,
                std::vector<CellProperty>& mappedProps);

private:
    // 计算四面体单元的中心点
    Point3D calculateTetCenter(const std::vector<Point3D>& nodes, 
                             const Tetrahedron& tet) const;
    
    // 计算角点网格单元的中心点
    std::vector<Point3D> calculateCornerGridCenters(
        const GridSize& size,
        const std::vector<std::vector<double>>& coord,
        const std::vector<double>& zcorn);
    
    // 执行插值计算
    void performInterpolation(
        const std::vector<Point3D>& sourceCenters,
        const std::vector<CellProperty>& sourceProps,
        const std::vector<Point3D>& targetCenters,
        std::vector<CellProperty>& mappedProps);
    
    // 计算目标点的插值结果
    CellProperty calculateInterpolation(
        const Point3D& target,
        const std::vector<std::pair<double, size_t>>& neighbors,
        const std::vector<CellProperty>& sourceProps);
    
    static size_t K_NEAREST_POINTS;  // K近邻搜索的邻居数量
    PropertyKDTree kdtree_;  // KD树用于快速空间搜索
}; 